package com.xl.studentpass.controller;

import com.xl.studentpass.entity.po.TaskOverViewPo;
import com.xl.studentpass.entity.vo.TaskVo;
import com.xl.studentpass.service.PublishTaskService;
import com.xl.studentpass.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author: xl
 * @createTime: 2023/5/27 14:10:16
 * @program: student-pass
 * @e-mail: 2199396150@qq.com
 * @description: 任务发布接口
 */
@RestController
@RequestMapping("/task")
@Slf4j
@Api(tags = "任务发布相关接口")
public class TaskController {
    @Resource
    PublishTaskService publishTaskService;

    @PostMapping("/create")
    @ApiOperation("创建任务")
    public R creatTask(@RequestBody TaskVo taskVo) {
        if (ObjectUtils.isEmpty(taskVo)) {
            return R.error("创建任务失败");
        }
        log.info("=============" + taskVo);
        return publishTaskService.create(taskVo);
    }

    /**
     * 获取订单详情
     */
    @GetMapping("/taskInfo")
    @ApiOperation("获取任务详细信息")
    public R getTaskInfo(@RequestParam("taskId") Integer taskId) {
        log.info("task==========" + taskId);
        return publishTaskService.getTaskInfo(taskId);
    }

    @GetMapping("/search")
    @ApiOperation("根据任务名搜索任务")
    private R searchTask(@RequestParam("taskName") String taskName) {
        List<TaskOverViewPo> task = publishTaskService.searchTask(taskName);
        return R.ok().put("data", task);
    }

    /**
     * 删除任务
     */
    @PostMapping("/delete")
    @ApiOperation("删除任务")
    public R deleteTask(@RequestParam String orderId) {
        return publishTaskService.delete(orderId);
    }

    @PostMapping("/confirm")
    @ApiOperation("确认订单完成")
    public R confirmTask(@RequestParam("taskId") Integer taskId) {
        if (ObjectUtils.isEmpty(taskId)) {
            return R.error();
        }
        return publishTaskService.confirmTask(taskId);
    }

    /**
     * 获取订单有效期
     */
    @GetMapping("/getValidTime")
    @ApiOperation("获取任务有效期")
    public R getValidTime(@RequestParam("taskId") String taskId) {
        Long validTime = publishTaskService.getValidTime(taskId);
        return R.ok().put("validSeconds", validTime);
    }

    /**
     * @param conditions {
     *                   "categoryId":"",
     *                   "pageNo":"",
     *                   "pageSize":"",
     *                   "priceStart":"",
     *                   "priceEnd":"",
     *                   }
     * @return R
     */
    @PostMapping("/screen")
    @ApiOperation("筛选数据")
    public R screenTask(@RequestBody Map<String, String> conditions) {
        List<TaskOverViewPo> taskInfos = publishTaskService.getScreenTask(conditions);
        return R.ok().put("data", taskInfos);
    }
}
